// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <gtest/gtest.h>

#include <pollux/testing/gtest_utils.h>
#include <pollux/common/testutil/test_value.h>
#include <pollux/external/tzdb/exception.h>
#include <pollux/type/tz/time_zone_map.h>

namespace kumo::pollux::tz {
namespace {

using namespace std::chrono;

DEBUG_ONLY_TEST(TimeZoneMapExternalInvalidTest, externalInvalid) {
  const int16_t testZoneId = 1681;
  const std::string testZone = "Africa/Abidjan";
  common::testutil::TestValue::enable();
  SCOPED_TESTVALUE_SET(
      "kumo::pollux::tz::locateZoneImpl",
      std::function<void(std::string_view * tz_name)>(
          [&](std::string_view* tz_name) -> void {
            if (*tz_name == testZone) {
              // Emulates an invalid_time_zone error thrown from external API
              // tzdb::locate_zone. In real scenarios, this could happen when
              // the timezone is not found in operating system's timezone list.
              throw tzdb::invalid_time_zone(std::string(*tz_name));
            }
          }));

  POLLUX_ASSERT_THROW(
      getTimeZoneName(testZoneId), "Unable to resolve timeZoneID");
  POLLUX_ASSERT_THROW(getTimeZoneID(testZone), "Unknown time zone");

  EXPECT_EQ("UTC", getTimeZoneName(0));
  EXPECT_EQ(0, getTimeZoneID("+00:00"));
  EXPECT_EQ("Africa/Accra", getTimeZoneName(1682));
  EXPECT_EQ(1682, getTimeZoneID("Africa/Accra"));
}
} // namespace
} // namespace kumo::pollux::tz
